package com.peanut.baby.mvp.feedback;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.mvp.BaseMvpActivity;
import com.peanut.baby.mvp.bind.BindMobileActivity;
import com.peanut.baby.mvp.login.LoginActivity;
import com.peanut.devlibrary.util.StringUtil;
import com.peanut.devlibrary.widget.TitleLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by anonymouslystudio on 2017/8/4.
 */

public class FeedbackActivity extends BaseMvpActivity implements TitleLayout.OnTitleClickListener,
        FeedbackContract.View, View.OnClickListener {
    private static final String TAG = FeedbackActivity.class.getSimpleName();

    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.feedback_edit)
    EditText feedbackEdit;
    @BindView(R.id.feedback_contact)
    EditText feedbackContact;
    @BindView(R.id.feedback_submit)
    TextView feedbackSubmit;

    private FeedbackContract.Presenter presenter;

    public static void start(Context ctx) {
        ctx.startActivity(new Intent(ctx, FeedbackActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        initViews();
        presenter = new FeedbackPresenter(this,this);
    }

    private void initViews() {
        title.setOnTitleClickedListener(this);
        title.setTitle("意见反馈");
        feedbackSubmit.setOnClickListener(this);
    }


    @Override
    public void onTitleBackClicked() {
        finish();
    }

    @Override
    public void onTitleRightClicked() {

    }

    @Override
    public void onTitleClicked() {

    }

    @Override
    public void onSubmitFeedbackSuccess() {
        dismissProgressDialog();
        showToast("提交成功");
        finish();
    }

    @Override
    public void onRequestFailed(String reason) {
        dismissProgressDialog();
        showToast("提交失败 "+reason);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.feedback_submit:
                submitFeedback();
                break;
        }
    }

    private void submitFeedback(){
        if (InitManager.getInstance().getUser() == null) {
            LoginActivity.startForResult(FeedbackActivity.this);
            return;
        }
        if (StringUtil.isNullOrEmpty(InitManager.getInstance().getUser().mobile)) {
            BindMobileActivity.startForResult(FeedbackActivity.this);
            return;
        }

        String content = feedbackEdit.getText().toString().trim();
        String contact = feedbackContact.getText().toString().trim();
        if(StringUtil.isNullOrEmpty(content)){
            showToast("请输入意见或建议");
            return;
        }
        showProgressDialog("请稍候...",false);
        presenter.submitFeedback(content,contact);
    }


}
